package com.fh.Set;
/*
定义一个Employee类，该类包含：private成员属性name，age要求：
1. 创建3个 Employee 对象放入 HashSet 中
2. 当name和age的值相同时，认为是相同员工，不能添加到HashSet集合当中
 */

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@SuppressWarnings({"all"})
public class HashSetExercise {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add(new Employee(25,"tom"));
        set.add(new Employee(20,"jack"));
        set.add(new Employee(25,"tom"));
        System.out.println(set);


    }
}
class Employee{
    private String name;
    private int age;
    public Employee(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "\n" + "Employee{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }//此处就是用来决定如果name和age的值相同，就返回相同的hash值
}
